﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Quản_Lý_Thư_Viện
{
    public partial class FrmDangNhap : Form
    {
        public FrmDangNhap()
        {
            InitializeComponent();
        }

        private void FrmDangNhap_Load(object sender, EventArgs e)
        {

        }
        //hehe
        public static string txt1 ,txt2,madg,matknv;
        public static int quyen;
        PhanquyenControl control = new PhanquyenControl();
        DocgiaControl dgcontrol = new DocgiaControl();
        PhanquyenInfo info = new PhanquyenInfo();
        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        private void btnOk_Click(object sender, EventArgs e)
        {
            txt1 = txtTenTaiKhoan.Text;
            txt2 = txtMatKhau.Text;
            dt = control.laydsphanquyen();
            if (txtTenTaiKhoan.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Mã Tài Khoản.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTenTaiKhoan.Focus();
                return;
            }
            if (txtMatKhau.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Mật Khẩu", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtMatKhau.Focus();
                return;
            }
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Mật Khẩu Hoặc Mã Tài Khoản Bị Sai.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DataRow dr3;
                dr3 = dt.Rows[0];
                if (Convert.ToInt16(dr3["QUYEN"].ToString()) == 0)
                {
                    dt2 = dgcontrol.laymataikhoan();
                    DataRow dr2 = dt2.Rows[0];
                    madg = dr2["MADG"].ToString();
                }

                if (Convert.ToInt16(dr3["QUYEN"].ToString()) == 1)
                {
                    matknv = dr3["MATAIKHOAN"].ToString();
                }

                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("Mật Khẩu Hoặc Mã Tài Khoản Bị Sai.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    string manv = dr["MATAIKHOAN"].ToString();
                    string pass = dr["MATKHAU"].ToString();
                    quyen = Convert.ToInt16(dr["QUYEN"].ToString());
                    if (txtTenTaiKhoan.Text != manv || txtMatKhau.Text != pass)
                    {
                        MessageBox.Show("Mật Khẩu Hoặc Mã Nhân Viên Bị Sai.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtTenTaiKhoan.Focus();
                        return;
                    }
                    else
                    {
                        this.Hide();
                        MessageBox.Show("Đăng Nhập Thành Công", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        frmChinh frm = new frmChinh();
                        frm.ShowDialog();
                        this.Close();
                    }
                }
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

    }
}
